using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using ScytheEngine;


namespace TestGameOnWindows
{
	static class Program
	{
        //Test scene manager using a test object
        class TestBackgroundObject : 
            ScytheEngine.Object, 
            ScytheEngine.IRenderable
        {
            Color m_BackgroundColor;

            public TestBackgroundObject(Color backgroundColor)
            {
                m_BackgroundColor = backgroundColor;
            }

            #region IRenderable Members

            public void Render(Microsoft.Xna.Framework.Graphics.GraphicsDevice gfxDev)
            {
                gfxDev.Clear(m_BackgroundColor);
            }

            #endregion
        }

        static ScytheEngineGame game = new ScytheEngineGame();

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		static void Main( string[] args )
		{
			// load all shaders
			Shader testShader = new Shader( @"Content/TransformTexture" );
			ShaderManager.AddShader( testShader, "TT" );

            game.GameStateManager.AddState(new GameStateOne());

			game.Run();

		}

		static void CameraControl()
		{
			Vector2 mouseMove = game.Input.MouseMoved;
			ButtonList mouseButtons = game.Input.MouseButtons;

			CameraManager.ActiveCamera.Revolve( Vector3.Zero, new Vector3( 1, 0, 0 ), mouseMove.Y * 0.01f );
			CameraManager.ActiveCamera.Revolve( Vector3.Zero, new Vector3( 0, 1, 0 ), mouseMove.X * 0.01f );

			if ( mouseButtons[0] == ButtonState.Pressed )
			{
				CameraManager.ActiveCamera.Translate( new Vector3( 0, 0, -1.3f ) );
			}

			if ( mouseButtons[2] == ButtonState.Pressed )
			{
				CameraManager.ActiveCamera.Translate( new Vector3( 0, 0, 1.3f ) );
			}
		}

        class GameStateOne : GameState
        {
            public override void LoadContent(bool loadAllContent)
            {
                ScytheEngineGame game = GameStateManager.Game as ScytheEngineGame;
                
				game.SceneManager.AddObject(new TestBackgroundObject(Color.DarkOrange));
				SEModel wedge = new SEModel( "Content/p1_wedge" );
				//wedge.SetShader( "TT" );
				game.SceneManager.AddObject( wedge );
				
				game.Input.OnUpdate += new UpdateMethod( SwitchToStateTwo );
				game.Input.OnUpdate += new UpdateMethod( CameraControl );
				base.LoadContent( loadAllContent );
            }

            public override void UnloadContent(bool unloadAllContent)
            {
                game.SceneManager.ClearScene();

                game.Input.OnUpdate -= new UpdateMethod(SwitchToStateTwo);
				game.Input.OnUpdate -= new UpdateMethod( CameraControl );

                base.UnloadContent(unloadAllContent);
            }

            void SwitchToStateTwo()
            {
                if (game.Input.Keys.Contains(Keys.F2))
                {
                    this.ExitScreen();
                    game.GameStateManager.AddState(new GameStateTwo());
                }
            }
        }

        class GameStateTwo : GameState
        {
            public override void LoadContent(bool loadAllContent)
            {
                ScytheEngineGame game = GameStateManager.Game as ScytheEngineGame;

				Quad testQuad = new Quad( @"Content/wall" );
				testQuad.SetShader( "TT" );
				testQuad.LoadContent( game.GraphicsDevice, game.Content );

				game.SceneManager.AddObject( new TestBackgroundObject( Color.DarkBlue ) );
				game.SceneManager.AddObject( testQuad );

                game.Input.OnUpdate += new UpdateMethod(GoFullScreen);
                game.Input.OnUpdate += new UpdateMethod(CameraControl);
                game.Input.OnUpdate += new UpdateMethod(SwitchToStateOne);
                
                base.LoadContent(loadAllContent);
            }

            public override void UnloadContent(bool unloadAllContent)
            {
                game.SceneManager.ClearScene();

                game.Input.OnUpdate -= new UpdateMethod(GoFullScreen);
                game.Input.OnUpdate -= new UpdateMethod(CameraControl);
                game.Input.OnUpdate -= new UpdateMethod(SwitchToStateOne);

                base.UnloadContent(unloadAllContent);
            }


            void GoFullScreen()
            {
                if (game.Input.Keys.Contains(Keys.F))
                {
                    game.GraphicsDeviceManager.ToggleFullScreen();
                }
            }

            void SwitchToStateOne()
            {
                if (game.Input.Keys.Contains(Keys.F1))
                {
                    this.ExitScreen();
                    game.GameStateManager.AddState(new GameStateOne());
                }
            }
        }
	}
}

